package com.higgins.lawapp.server.dataModel;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Key;
import com.higgins.lawapp.client.settings.dataModel.DataSourceDTO;
import com.higgins.lawapp.client.settings.dataModel.DataSourceLevelDTO;


@PersistenceCapable
public class DataSource {
	@SuppressWarnings("unused")
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Key key;

	@Persistent
	private String dataSourceName;

	@Persistent
	private String dataSourceURL;

	@Persistent
	private Boolean dataSourceExecute;

	@Persistent
	private List<DataSourceLevel> dataSourceLevels;

	public DataSource()	{
	}
	
	public DataSource(DataSourceDTO dataSourceDTO)	{
		this.dataSourceName = dataSourceDTO.getDataSourceName();
		this.dataSourceURL = dataSourceDTO.getDataSourceURL();		
		this.dataSourceExecute = dataSourceDTO.getDataSourceExecute();
		
		List<DataSourceLevelDTO> dataSourceLevelDTOs = dataSourceDTO.getDataSourceLevelDTOs();
		Iterator<DataSourceLevelDTO> iterator = dataSourceLevelDTOs.iterator();
		int index = 0;
		while(iterator.hasNext())	{
			DataSourceLevelDTO dataSourceLeveDTO = iterator.next();
			DataSourceLevel dataSourceLevel = new DataSourceLevel(dataSourceLeveDTO);
			if (index==0)
				dataSourceLevels = new ArrayList<DataSourceLevel>();
			dataSourceLevels.add(dataSourceLevel);
			index++;
		}
		
	}
	public void setDataSourceName(String dataSourceName)	{
		this.dataSourceName = dataSourceName;
	}
	public String getDataSourceName()	{
		return dataSourceName;
	}
	public void setDataSourceURL(String dataSourceURL)	{
		this.dataSourceURL = dataSourceURL;
	}
	public String getDataSourceURL()	{
		return dataSourceURL;
	}
	public void setDataSourceExecute(Boolean dataSourceExecute)	{
		this.dataSourceExecute = dataSourceExecute;
	}
	public Boolean getDataSourceExecute()	{
		return dataSourceExecute;
	}
	public void addDataSourceLevel(DataSourceLevel dataSourceLevel)	{
		if (dataSourceLevels==null)
			dataSourceLevels = new ArrayList<DataSourceLevel>();
		dataSourceLevels.add(dataSourceLevel);
	}
	public List<DataSourceLevel> getDataSourceLevels()	{
		return dataSourceLevels;
	}
	public DataSourceLevel getDataSourceLevel(int index)	{
		return dataSourceLevels.get(index);
	}
	
	
	
	public DataSourceDTO getDataSourceDTO()	{
		DataSourceDTO dataSourceDTO = new DataSourceDTO();
		dataSourceDTO.setDataSourceName(dataSourceName);
		dataSourceDTO.setDataSourceNameKey(dataSourceName);
		dataSourceDTO.setDataSourceExecute(dataSourceExecute);
		
		Iterator<DataSourceLevel> iterator = dataSourceLevels.iterator();
		while (iterator.hasNext())	{
			DataSourceLevel dataSourceLevel = iterator.next();
			DataSourceLevelDTO dataSourceLevelDTO = dataSourceLevel.getDataSourceDTO();
			dataSourceDTO.addDataSourceLevelDTO(dataSourceLevelDTO);
		}
		
		return dataSourceDTO;
	}
	public void update(DataSourceDTO dataSourceDTO)	{			// THESE METHODS NEED COMPLETING FOR THE 
		this.dataSourceName = dataSourceDTO.getDataSourceName();
		this.dataSourceURL = dataSourceDTO.getDataSourceURL();		
		this.dataSourceExecute = dataSourceDTO.getDataSourceExecute();
		
		List<DataSourceLevelDTO> dataSourceLevelDTOs = dataSourceDTO.getDataSourceLevelDTOs();
		Iterator<DataSourceLevelDTO> iterator = dataSourceLevelDTOs.iterator();
		int index = 0;
		while(iterator.hasNext())	{
			DataSourceLevelDTO dataSourceLevelDTO = iterator.next();
			DataSourceLevel dataSourceLevel=null; 
			if (dataSourceLevels.size()<=index)	{
				dataSourceLevel = new DataSourceLevel(dataSourceLevelDTO);
				dataSourceLevels.add(dataSourceLevel);
			}	else	{
				dataSourceLevel = dataSourceLevels.get(index);
				dataSourceLevel.update(dataSourceLevelDTO);
			}
			index++;
		}
	}
}
